package com.example.coursedesign.ui.notifications;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import com.example.coursedesign.R;
import com.example.coursedesign.databinding.FeedbackBinding;

import java.io.IOException;

public class FeedbackActivity extends AppCompatActivity {

    private FeedbackBinding binding;
    private Uri imageUri = null;
    private String selectedTag = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = FeedbackBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 启用返回按钮
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        // 设置标签按钮点击事件
        binding.featureSuggestionButton.setOnClickListener(v -> selectTag("功能建议"));
        binding.performanceIssueButton.setOnClickListener(v -> selectTag("性能问题"));

        // 设置上传图片按钮点击事件
        binding.buttonAddImage.setOnClickListener(v -> openImagePicker());

        // 设置删除图片按钮点击事件
        binding.buttonRemoveImage.setOnClickListener(v -> removeImage());

        // 设置提交按钮点击事件
        binding.buttonSubmit.setOnClickListener(v -> submitFeedback());
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed(); // 返回上一个 Activity
        return true;
    }

    private void selectTag(String tag) {
        // 选择标签并更新选中状态
        if (tag.equals("功能建议")) {
            binding.featureSuggestionButton.setSelected(true);
            binding.performanceIssueButton.setSelected(false);
        } else if (tag.equals("性能问题")) {
            binding.performanceIssueButton.setSelected(true);
            binding.featureSuggestionButton.setSelected(false);
        }
        selectedTag = tag;

        // 更新按钮背景和文本颜色
        updateButtonAppearance();
    }

    private void updateButtonAppearance() {
        // 根据选中状态更新按钮的背景和文本颜色
        binding.featureSuggestionButton.setBackgroundTintList(getResources().getColorStateList(R.drawable.btn_background_selector));
        binding.performanceIssueButton.setBackgroundTintList(getResources().getColorStateList(R.drawable.btn_background_selector));

        binding.featureSuggestionButton.setTextColor(getResources().getColorStateList(R.color.btn_text_color_selector));
        binding.performanceIssueButton.setTextColor(getResources().getColorStateList(R.color.btn_text_color_selector));
    }

    private void openImagePicker() {
        // 打开图片选择器
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        imagePickerLauncher.launch(intent);
    }

    private final ActivityResultLauncher<Intent> imagePickerLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK && result.getData() != null) {
                    imageUri = result.getData().getData();
                    try {
                        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
                        binding.imageView.setImageBitmap(bitmap);
                        binding.imageView.setVisibility(View.VISIBLE);
                        binding.buttonRemoveImage.setVisibility(View.VISIBLE);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

    private void removeImage() {
        // 移除图片
        binding.imageView.setImageDrawable(null);
        binding.imageView.setVisibility(View.GONE);
        binding.buttonRemoveImage.setVisibility(View.GONE);
        imageUri = null;
    }

    private void submitFeedback() {
        String feedbackContent = binding.feedbackContent.getText().toString().trim();
        String contactInfo = binding.contactInfo.getText().toString().trim();
        if (selectedTag.isEmpty()) {
            Toast.makeText(this, "请选择标签", Toast.LENGTH_SHORT).show();
            return;
        }
        if (feedbackContent.isEmpty()) {
            Toast.makeText(this, "请填写反馈内容", Toast.LENGTH_SHORT).show();
            return;
        }
        if (contactInfo.isEmpty()) {
            Toast.makeText(this, "请填写联系方式", Toast.LENGTH_SHORT).show();
            return;
        }
        Toast.makeText(this, "提交成功！", Toast.LENGTH_SHORT).show();
        binding.feedbackContent.setText("");
        binding.contactInfo.setText("");
        if (imageUri != null) {
            removeImage();
        }
        selectedTag = "";
        binding.featureSuggestionButton.setSelected(false);
        binding.performanceIssueButton.setSelected(false);
        updateButtonAppearance();
    }
}